package com.bid.printHtml.service;

import cn.hutool.core.date.DateUtil;
import com.bid.epp.pbxzsz.model.BidXmzj;
import com.bid.epp.pbxzsz.service.BidPbxzszService;
import com.bid.epp.zgfhx.model.BidZgfhResult;
import com.bid.epp.zgfhx.service.BidZgfhResultService;
import com.bid.printHtml.base.BasePrintWriter;
import com.bid.printHtml.entity.PrintEntity;
import com.bid.printHtml.entity.TreeChildren;
import com.bid.printHtml.entity.TreeVo;
import com.bid.project.model.BidBzxx;
import com.bid.project.model.BidReceiveHistory;
import com.bid.project.model.BidXmxx;
import com.bid.project.service.BidReceiveHistoryService;
import com.bid.project.service.BidXmxxService;
import com.fxoa.base.model.FxResponse;
import com.porting.TemplateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liangyonghao
 * @date 2022年08月09日上午 9:43
 */
@Service
public class PrintHtml extends BasePrintWriter {
    @Autowired
    private BidXmxxService xmxxService;
    @Autowired
    private BidZgfhResultService zgfhResultService;
    @Autowired
    private BidPbxzszService pbxzszService;
    @Autowired
    private BidReceiveHistoryService receiveHistoryService;

    @Override
    public HashMap generate(String referId, String TemplatePath) {
        HashMap hashMap =new HashMap();
        TreeVo treeVo =new TreeVo();
        BidBzxx bidBzxx = xmxxService.getBidbzxxById(referId);
        BidXmxx bidXmxx = xmxxService.getBidXmxxById(bidBzxx.getXmid());
        // 获取供应商列表
        List<BidReceiveHistory> supplierList = receiveHistoryService.findByPackId(referId);
        //项目编号
        String projectCode = bidXmxx.getXmbh();
        if ("2".equals(bidXmxx.getSybhlx())) {
            if (StringUtils.isNotBlank(bidXmxx.getWbbh())) {
                projectCode = bidXmxx.getWbbh();
            }
        }
        List<BidZgfhResult> zgfhResultList1 = zgfhResultService.singleCompute(referId, null, null);//计算
        // 获取该包组的所有专家
        BidXmzj xmzjParam = new BidXmzj();
        xmzjParam.setBzid(referId);
        xmzjParam.setPszt("1");
        List<BidXmzj> xmzjList = pbxzszService.queryXmzj(xmzjParam);

        List<BidXmzj> xmzjListResult = zgfhResultService.xmzjComputeZgfhResult(xmzjList,referId, supplierList.get(0).getSupplierId());

        Map<String, Object> params = new HashMap<>();
        params.put("xmmc", bidXmxx.getXmmc());
        params.put("xmbh", projectCode);
        params.put("hasTwoLine",true);
        params.put("pbdd", "协商地点："+bidBzxx.getKbsfj());
        params.put("pbsj", "协商时间："+ DateUtil.format(bidXmxx.getKbsj(), "YYYY年MM月dd日"));
        params.put("supplierList",supplierList);
        params.put("xmzjList",xmzjListResult);
        params.put("zgfhResultList",zgfhResultList1);
        params.put("date",DateUtil.format(DateUtil.date(), "YYYY年MM月dd日"));
        params.put("i",new List[4]);
        String content = TemplateUtils.generateTemplate(TemplatePath, params);
        hashMap.put("content",content);
        treeVo.setTitle("初审结果汇总表");
        treeVo.setTemplatePath("webFtl/templates.ftl");
        treeVo.setClassWriteKey("printTest");

        //子
        TreeChildren treeChildren =new TreeChildren();
        treeChildren.setTitle("子数据");
        treeChildren.setClassWriteKey("printTest");
        treeChildren.setTemplatePath("webFtl/templates.ftl");
        treeChildren.setKey("1");
        List<TreeChildren>  treeChildrenList=new ArrayList<>();
        treeChildrenList.add(treeChildren);
        treeVo.setChildren(treeChildrenList);
        List<PrintEntity> printEntityList =new ArrayList<>();
        printEntityList.add(treeVo);

        hashMap.put("printData",printEntityList);
        return hashMap;
    }
}
